package cn.lil.dao;

import cn.lil.model.Bearing;
import cn.lil.model.OrderItem;
import cn.lil.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class OrderItemDao {
    public List<OrderItem> getItemByOrderId(int orderId) throws SQLException {
        String sql = "select b.imgSrc,o.bearing_id AS bearingId, o.order_id AS orderId,o.id,o.price,o.amount from " +
                "orderitem o,bearing b where o.order_id=? and o.bearing_id=b.id";
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        return r.query(sql, new BeanListHandler<>(OrderItem.class),orderId);
    }

    public List<OrderItem> getItemById(int id) throws SQLException {
        String sql = "select b.imgSrc,o.bearing_id AS bearingId, o.order_id AS orderId,o.id,o.price,o.amount from " +
                "orderitem o,bearing b where o.id=? and o.bearing_id=b.id";
        QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
        return r.query(sql, new BeanListHandler<>(OrderItem.class),id);
    }
}
